/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import ohos.agp.colors.RgbColor;

import com.cv4j.core.datamodel.ImageData;
import com.cv4j.core.datamodel.ImageProcessor;

import java.util.Optional;

/**
 * 过滤器
 *
 * @author dev
 * @date 2021/07/22
 */
public class SinCityFilter extends BaseFilter {
    private double threshold = 200; // default value
    private RgbColor mainColor = new RgbColor(255, 0x00, 0x00);

    /**
     * 设置主要颜色
     *
     * @param argb argb
     */
    public void setMainColor(int argb) {
        this.mainColor = new RgbColor(argb);
    }

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link Optional<ImageProcessor>}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        int total = width * height;
        int tr = 0;
        int tg = 0;
        int tb = 0;
        for (int i1 = 0; i1 < total; i1++) {
            tr = RED[i1] & 0xff;
            tg = GREED[i1] & 0xff;
            tb = BLUE[i1] & 0xff;
            int gray = (int) (0.299 * (double) tr + 0.587 * (double) tg + 0.114 * (double) tb);
            double distance = getDistance(tr, tg, tb);
            if (distance < threshold) {
                double k1 = distance / threshold;
                int[] rgb = getAdjustableRGB(tr, tg, tb, gray, (float) k1);
                tr = rgb[0];
                tg = rgb[1];
                tb = rgb[2];
                RED[i1] = (byte) tr;
                GREED[i1] = (byte) tg;
                BLUE[i1] = (byte) tb;
            } else {
                RED[i1] = (byte) gray;
                GREED[i1] = (byte) gray;
                BLUE[i1] = (byte) gray;
            }
        }
        return Optional.ofNullable(src);
    }

    /**
     * 得到可调rgb
     *
     * @param tr tr
     * @param tg tg
     * @param tb 结核病
     * @param gray 灰色的
     * @param rate 率
     * @return {@link int[]}
     */
    private int[] getAdjustableRGB(int tr, int tg, int tb, int gray, float rate) {
        int[] rgb = new int[3];
        rgb[0] = (int) (tr * rate + gray * (1.0f - rate));
        rgb[1] = (int) (tg * rate + gray * (1.0f - rate));
        rgb[2] = (int) (tb * rate + gray * (1.0f - rate));
        return rgb;
    }

    /**
     * 得到的距离
     *
     * @param tr tr
     * @param tg tg
     * @param tb 结核病
     * @return double
     */
    private double getDistance(int tr, int tg, int tb) {
        RgbColor rgbColor = new RgbColor(mainColor);
        int dr = tr - rgbColor.getRed();
        int dg = tg - rgbColor.getGreen();
        int db = tb - rgbColor.getBlue();
        int distance = ImageData.SQRT_LUT[Math.abs(dr)] +
            ImageData.SQRT_LUT[Math.abs(dg)] +
            ImageData.SQRT_LUT[Math.abs(db)];
        return Math.sqrt(distance);
    }
}
